package microarray;

import java.io.IOException;

public abstract class Downloader
{
	// The purpose of this class is to simply remove the
	// boilerplate restart junk from the rest of the code.
	// All you need to do is subclass it and provide a
	// function called download, and then simply call
	// new MyDownloader().doDownload(name, sleep, retries);
	public abstract void download() throws IOException;

	public void doDownload(String name, int sleepTime, int retriesLeft)
			throws IOException
	// recursive. Stack depth is limited to the size of retriesLeft.
	{
		try
		{
			download();
		} catch (IOException e)
		{
			if (retriesLeft > 0)
			{
				System.out.println("Download " + name
						+ " failed. Trying again in " + sleepTime
						+ "ms. Retries left: " + retriesLeft);
				try
				{
					Thread.sleep(sleepTime);
				} catch (InterruptedException e1)
				{
				}
				retriesLeft--;
				doDownload(name, sleepTime, retriesLeft);
			} else
			{
				System.out.println("Giving up on " + name);
				throw (e);
			}
		}
	}
}